﻿/**************************************************************************************************************
 * Class name:  EnumHelper.cs
 * Description: 枚举助手
 * Author:      Ju2ender
 * Create:      2012-10-29 Monday 18:03:23
 * Update:      2012-10-29 Monday 18:03:23
 **************************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using DearLee.Toolkit.Web;

namespace DearLee.Toolkit.Enums {
  
  /// <summary>
  /// 枚举助手
  /// </summary>
  public class EnumHelper {

    /// <summary>
    /// 列出给定枚举类型的全部值项
    /// </summary>
    /// <param name="enumType">枚举类型</param>
    /// <returns></returns>
    public static List<ListItem> ListEnum(Type enumType) {

      List<ListItem> list = null;

      list = new List<ListItem>();

      foreach (string item in Enum.GetNames(enumType)) {
        list.Add(new ListItem {
          Text = item.Contains('_') ? item.Replace('_', ' ') : item,
          Value = Convert.ToInt32(Enum.Parse(enumType, item)).ToString()
        });
      }

      return list;
    }

    /// <summary>
    /// 根据枚举名称获得该枚举的值
    /// </summary>
    /// <param name="enumType">枚举类型</param>
    /// <param name="enumText">枚举名称</param>
    /// <returns>该枚举的值</returns>
    public static int GetEnumValueByName(Type enumType, string enumText) {
      return Convert.ToInt32(Enum.Parse(enumType, enumText));
    }

    /// <summary>
    /// 枚举枚举值获得该枚举的名称
    /// </summary>
    /// <param name="enumType">枚举类型</param>
    /// <param name="enumValue">枚举值</param>
    /// <returns>枚举名称</returns>
    public static string GetEnumNameByValue(Type enumType, int enumValue) {

      string enumText = string.Empty;
      
      enumText = Enum.Parse(enumType, enumValue.GetHashCode().ToString()).ToString();
      enumText = enumText.Contains('_') ? enumText.Replace('_', ' ') : enumText;

      return enumText;
    }
  }
}
